%% Paths and startup


path() %returns MATLAB's current path and search order.  
%MATLAB searches for scripts and functinos starting with the 'Current Folder'
% and then looks in each directory in the path, starting at the top. 

%The first function or script it finds with the correct name is used. 

%Add a path (full):
addpath('C:\Users\klidke\Dropbox\klidke\UNM Courses\290 Spring 2016\ClassNotes')

%Add a path (relative to Current Folder):
addpath('ClassNotes')

%change to a directory:
cd('C:\Users\klidke\Dropbox\klidke\UNM Courses\290 Spring 2016\ClassNotes')


%Find the first folder on the path that contains 'Feb_1.m'
which Feb_1

%The file 'startup.m' located in MyDocuments\MATLAB\ on Windows is run
%automatically by MATLAB just after startup.  You may want to add paths
%using 'addpath('...') statements in your 'startup.m'

edit startup

%% Display the rectangles being used for Midpoint method using 'bar'

%Limits of integration
A=0
B=10

% This shows what our function looks like
FineSample=linspace(A,B,1000);
FineSine=sin(FineSample); %We are using the 'sin' function here

% Now we will integrate 'sin(x)' from A to B
NInterval = 10; %Number of intervals 

%We can create an array of midpoints this way:
EndPoints = linspace(A,B,NInterval+1)% +1 because we have end points
DeltaX = EndPoints(2)-EndPoints(1) %Spacing of intevals
MidPoints = EndPoints(1:end-1)+DeltaX/2 %Note the 'end-1' indexing

%The function evaluated at the mid points
FMid=sin(MidPoints)

figure
B=bar(MidPoints,FMid) %Make a bar graph type figure
B.BarWidth=1;  %Set the bar width to 1 (default is 0.8)
hold on
plot(FineSample,FineSine,'r') %Plot function
xlabel('X')
ylabel('f(X)')
legend('Boxes used in MidPoint','Smooth Function')


%% Numerical Integration: Trapezoid Method

web('https://en.wikipedia.org/wiki/Trapezoidal_rule')

%Use an anonymous function so we can change what we are integrating in one
%place
FuncToInt = @(X)exp(-X); 

%Limits of integration
A=0
B=10

% Now we will integrate our function from A to B
NInterval = 100; %Number of intervals 

%We can create an array of midpoints this way:
EndPoints = linspace(A,B,NInterval+1)% +1 because we have end points
DeltaX = EndPoints(2)-EndPoints(1) %Spacing of intevals
MidPoints = EndPoints(1:end-1)+DeltaX/2 %Note the 'end-1' indexing

%Setup our loop for integrating
IntResult=0; %The accumulated intergral results
IntResult_Trap=0; %The accumulated intergral results

for nn=1:NInterval %loop over midpoints
    
    %This is for the mid point method
    Area  = FuncToInt(MidPoints(nn))*DeltaX; %This is the rectangular area
    IntResult = IntResult +Area; %Add that to our accumulated integral
    
    %This is for the trapazoid method
    H = (FuncToInt(EndPoints(nn))+FuncToInt(EndPoints(nn+1)))/2;
    IntResult_Trap = IntResult_Trap+ H*DeltaX;
end

%Compare our result to the exact analytical result.  

%AnalyticResult = -cos(B) + cos(A);   %for sin(x)
AnalyticResult = -exp(-B) + exp(-A);  %for exp(-x)

%Use fprintf to show formatted results. 'help fprintf'

fprintf('\n\n')
fprintf('Analytic Result: %g \n',AnalyticResult)
fprintf('Mid point: %g \n',IntResult)

fprintf('Trapazoid: %g \n',IntResult_Trap)

fprintf('Error MidPoint: %g \n',AnalyticResult-IntResult)
fprintf('Error Trapazoid: %g \n',AnalyticResult-IntResult_Trap)







